<?php

namespace App\Admin\Controllers;

use App\Models\CollPosition;
use App\Models\Product;
use App\Models\ProductAttrKey;
use App\Models\ProductCategory;
use App\Models\StoreUser;
use App\Models\SweetnessList;
use Encore\Admin\Controllers\AdminController;
use App\Admin\Actions\Post\Restore;
use App\Admin\Actions\Post\productSKUList;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Show;


class ProductController extends AdminController
{
    protected $title = '商品列表';

    protected function grid()
    {
        $grid = new Grid(new Product());

        $grid->column('id', __('ID号'));
//        $grid->column('sort', __('排序'));
        $grid->column('product_name', __('商品名称'));

        $grid->column('category_id', __('商品分类'))->display(function ($category_id) {
            $name = ProductCategory::where('id', $category_id)->value('name');
            return $name;
        });
        $grid->column('cover_ico', __('封面图'))->image(self::$baseImg, 100, 100);
        $grid->column('pics', '主图')->image(self::$baseImg, 100, 100);
        $grid->column('member_price', __('价格'));
        $grid->column('sell_num', __('销量'));

        $grid->column('sid', '所属商户')->display(function ($sid) {
            return StoreUser::where('sid', $sid)->value('username');
        });

        $grid->column('is_hot', '是否爆款?')->display(function ($is_hot) {
            return $is_hot ? '是' : '否';
        });
        $grid->column('is_new', '是否新品?')->display(function ($is_new) {
            return $is_new ? '是' : '否';
        });
        $grid->column('is_famous_business', '是否名企?')->display(function ($is_new) {
            return $is_new ? '是' : '否';
        });
        $grid->column('is_famous_wine', '是否名酒?')->display(function ($is_new) {
            return $is_new ? '是' : '否';
        });
        $grid->column('is_famous_brand', '是否名家?')->display(function ($is_new) {
            return $is_new ? '是' : '否';
        });
        $grid->column('is_tea', '是否茶叶?')->display(function ($is_new) {
            return $is_new ? '是' : '否';
        });
//        $grid->column('num', __('余量'));
        $grid->column('status', __('状态'))->using(['-1' => '删除', '0' => '未上架', '1' => '正常']);

        $grid->column('created_at', __('创建时间'));
        $grid->column('updated_at', __('更新时间'));

        $grid->disableExport();
        $grid->disableColumnSelector();

        $grid->filter(function ($filter) {
            // 去掉默认的id过滤器
            $filter->disableIdFilter();

            // 在这里添加字段过滤器
            $filter->like('product_name', '商品名称');

            $filter->where(function ($query) {

                $idsObj = ProductCategory::where('name', 'like', "%{$this->input}%")->pluck('id');
                $idsArr = [];
                if ($idsObj) {
                    foreach ($idsObj as $v) {
                        array_unshift($idsArr, $v);
                    }
                }

                $ids = array_unique($idsArr);

                $query->whereIn('category_id', $ids);

            }, '商品分类');

        });

        $grid->actions(function ($actions) {
            // 去掉删除
            $actions->disableDelete();
        });

        return $grid;
    }

    protected function detail($id)
    {
        $show = new Show(Product::findOrFail($id));

//        $show->field('id', __('Id号'));
        $show->field('sort', __('排序'));
        $show->field('product_name', __('商品名称'));

        $show->field('category_id', __('商品分类'))->as(function ($category_id) {
            $name = ProductCategory::where('id', $category_id)->value('name');
            return $name;
        });

        $show->field('cover_ico', __('封面图'))->image();
        $show->field('pics', __('主图'))->carousel();
        $show->field('product_introduct', __('商品简介'));
        $show->field('shipping', __('配送'));
        $show->field('security', __('保障'));
        $show->field('from', __('产地'));
        $show->field('member_price', __('价格'));
        $show->field('sell_num', __('销量'));
        $show->field('sweetness', __('甜度'))->as(function ($sweetness) {
            $name = SweetnessList::where('id', $sweetness)->value('name');
            return $name;
        });

        $show->field('sid', __('所属商户'))->as(function ($sid) {
            return StoreUser::where('sid', $sid)->value('username');
        });

//        $show->field('num', __('余量'));
        $show->field('is_hot', '是否爆款')->using(['1' => '是', '0' => '否']);
        $show->field('is_new', '是否新品')->using(['1' => '是', '0' => '否']);

        $show->field('is_famous_business', '是否名企')->using(['1' => '是', '0' => '否']);
        $show->field('is_famous_wine', '是否名酒')->using(['1' => '是', '0' => '否']);
        $show->field('is_famous_brand', '是否名家')->using(['1' => '是', '0' => '否']);
        $show->field('is_tea', '是否茶叶')->using(['1' => '是', '0' => '否']);

        $show->field('status', __('状态'))->using(['-1' => '删除', '0' => '未上架', '1' => '正常']);

        $show->field('created_at', __('创建时间'));
        $show->field('updated_at', __('更新时间'));

        $show->panel()
            ->tools(function ($tools) {
                $tools->disableDelete();
            });

        $show->product_sku('规格', function ($attrs) {

            $attrs->resource('/admin/product_sku');

            $attrs->value('规格值');
            $attrs->original_price('原价');
            $attrs->member_price('现价');
            $attrs->number('数量');

            $attrs->disableExport();
            $attrs->disableColumnSelector();
            $attrs->disableFilter();
            $attrs->disableRowSelector();
        });

        return $show;
    }

    protected function form()
    {
        $form = new Form(new Product());

        $form->number('sort', __('排序'));
        $form->text('product_name', __('商品名称'))->required();

        $cates = ProductCategory::select('id', 'name')->get();
        $catesArr = [];
        foreach ($cates as $v) {
            $catesArr[$v->id] = $v->name;
        }

        $form->select('category_id', __('商品分类'))->options($catesArr)->required();
        $form->text('product_introduct', __('商品简介'))->required();
        $form->text('shipping', __('配送'))->required();
        $form->text('security', __('保障'))->required();
        $form->text('from', __('产地'))->required();
        $form->text('member_price', __('价格'))->required();
        $form->number('sell_num', __('销量'))->required();
        $form->image('cover_ico', __('封面图'))->required();
        $form->multipleImage('pics', '主图')->removable();
        $sweetnessList = SweetnessList::select('id', 'name')->get();
        $sweetnessArr = createOptions($sweetnessList);
        $form->select('sweetness', '甜度')->options($sweetnessArr);

        $sids_arr = StoreUser::where('status', 1)->select('sid', 'username')->get();
        $sdata = [];
        foreach ($sids_arr as $val) {
            $sdata[$val['sid']] = $val['username'];
        }
        $form->select('sid', __('所属商户'))->options($sdata);

        $states = [
            'on' => ['value' => 1, 'text' => '是', 'color' => 'success'],
            'off' => ['value' => 0, 'text' => '否', 'color' => 'danger'],
        ];

        $form->switch('is_hot', '是否爆款')->states($states);
        $form->switch('is_new', '是否新品')->states($states);
        $form->switch('is_famous_business', '是否名企')->states($states);
        $form->switch('is_famous_wine', '是否名酒')->states($states);
        $form->switch('is_famous_brand', '是否名家')->states($states);
        $form->switch('is_tea', '是否茶叶')->states($states);

        $form->list('grape_variety', '葡萄品种');

        $form->table('base_parameter', '产品参数', function ($table) {
            $table->text('key', '参数名');
            $table->text('value', '参数值');
        });

//        $form->table('grape_variety', '葡萄品种', function ($table) {
//            $table->text('参数名');
//            $table->text('参数值');
//        });

        $form->table('wine_taster', '品酒', function ($table) {
            $table->text('key', '参数名');
            $table->text('value', '参数值');
        });

        $form->table('package', '包装', function ($table) {
            $table->text('key', '参数名');
            $table->text('value', '参数值');
        });

        $form->UEditor('product_detail', '图文描述')->options(['initialFrameHeight' => 800]);

        $status = [
            -1 => '删除',
            0 => '未上架',
            1 => '正常',
        ];

        $form->radio('status', '状态')->options($status)->default(1);

        $form->tools(function (Form\Tools $tools) {

            // 去掉`删除`按钮
            $tools->disableDelete();

        });

        $form->footer(function ($footer) {
            // 去掉`继续编辑`checkbox
            $footer->disableEditingCheck();
        });

        return $form;
    }
}